<?php
namespace app\index\controller;

use think\Request;

class Index extends Common2
{
    public function index()
    {
        //输出轮播图
        $list = db('ad')->order('num asc')->select();
        $this->assign('list',$list);

        //输出分类
        $fenlei = db('goods_fenlei')->where("grade=1")->order('id asc')->select();
        $this->assign('fenlei',$fenlei);

        //输出热销商品
        $goods_list = db('goods')->alias('a')->join('goods_attributes b','a.id=b.goods_id')->field('a.*,a.id as goodsid,b.price')->where("is_onsale=1")->order('price asc')->limit(8)->select();
        // dump($goods_list);die;
        $row=[];
        foreach( $goods_list as $val )
        {
            $img = json_decode($val['src'],true);    //转换格式,json字符串->php数组
            if($img!=NULL){
                $val['src']=array($img[0]);
            }else{
                $val['src']=array($val['src']);
            }
            $thumb_img = json_decode($val['thumb_src'],true);    //转换格式,json字符串->php数组
            if($thumb_img!=NULL){
                $val['thumb_src']=array($img[0]);
            }else{
                $val['thumb_src']=array($val['thumb_src']);
            }
            $row[] = $val;  //存入新数组$row中
        }
        // dump($row);die;
        $this->assign('goodslist',$row);

        // 首页分类1图片
        $fenlist = db('goods_fenlei')->alias('a')->join('goods b','b.fenlei_id=a.id')->field('a.pid,b.*')->where('a.pid=3')->select();
        $fenlei1 = [];
        foreach($fenlist as $val)
        {
            $img = json_decode($val['src'],true);
            if($img!=NULL){
                $val['src']=array($img[0]);
            }else{
                $val['src']=array($val['src']);
            }
            $fenlei1[] = $val;
        }
        // dump($fenlei1);die;
        $this->assign('fenlei1',$fenlei1);

        // 首页分类2图片
        $fenlist2 = db('goods_fenlei')->alias('a')->join('goods b','b.fenlei_id=a.id')->field('a.pid,b.*')->where('a.pid=9')->select();
        $fenlei2 = [];
        foreach($fenlist2 as $val)
        {
            $img = json_decode($val['src'],true);
            if($img!=NULL){
                $val['src']=array($img[0]);
            }else{
                $val['src']=array($val['src']);
            }
            $fenlei2[] = $val;
        }
        // dump($fenlei2);die;
        $this->assign('fenlei2',$fenlei2);

        return view();
    }
    //首页分类入口
    public function product()
    {
        // $pageNow = $list->currentPage;
        // $page = str_replace([
        //     'class="pagination"',
        //     'page',
        //     '<span>'.$pageNow.'</span>',
        //     'active',
        //     '<li class="disabled"><span>&laquo;</span>',
        //     '<li class="disabled"><span>&raquo;</span>',
        // ],[
        //     '',
        //     'fenlei_id='.$fenlei_id.'&page',
        //     '<a href="/index/index/product.html?fenlei_id='.$fenlei_id.'&page='.$pageNow.'">'.$pageNow.'</a>',
        //     'disabled',
        //     '<li><a>&laquo;</a>',
        //     '<li><a>&raquo;</a>',
        // ],$page);
        $fenlei_id = input('get.fenlei_id');
        $grade_one_fenlei = db('goods_fenlei')->where("grade=1")->order('id asc')->select();
        $this->assign('grade_one_fenlei',$grade_one_fenlei);
        $this->assign('fenlei_id',$fenlei_id);
        //获取二级分类
        $grade_two_fenlei = db('goods_fenlei')->where("grade=2 and pid={$fenlei_id}")->order('id asc')->select();
        $this->assign('grade_two_fenlei',$grade_two_fenlei);
        return view();
    }
    public function goodsList($fenlei_id){
        //滚动加载方法
        $pageNo = $_GET['pageNo'];
        $pagesize = 6;  //每页显示的个数
        $page = input('param.page',$pageNo); //当前页数
        $left = ($page-1)*$pagesize;    //limit查询的第一个条件
        //内置标签输出商品
        //判断传来的$fenlei_id是一级分类还是二级分类
        $gradeOne_data = db('goods_fenlei')->where("grade=1")->order('id asc')->select();
        $gradeOneId_list = [];
        foreach ($gradeOne_data as $k => $v) {
            $gradeOneId_list[] = $v['id'];
        }
        if(in_array($fenlei_id,$gradeOneId_list)){
            $data = db('goods_fenlei')->alias('a')->join('goods b','a.id=b.fenlei_id')->field('b.*')->where("a.pid={$fenlei_id}")->limit($left,$pagesize)->select();
        }else{
            $data = db('goods_fenlei')->alias('a')->join('goods b','a.id=b.fenlei_id')->field('b.*')->where("a.id={$fenlei_id}")->limit($left,$pagesize)->select();
        }               
        //数组重组,加入所需要的元素
        if(empty($data)){
            $this->error("没有了");
        }
        $row=[];
        foreach( $data as $val )
        {
            $img = json_decode($val['src'],true);    //转换格式,json字符串->php数组
            if($img!=NULL){
                $val['src']=array($img[0]);
            }else{
                $val['src']=array($val['src']);
            }
            $thumb_img = json_decode($val['thumb_src'],true);    //转换格式,json字符串->php数组
            if($thumb_img!=NULL){
                $val['thumb_src']=array($img[0]);
            }else{
                $val['thumb_src']=array($val['thumb_src']);
            }
            $row[] = $val;  //存入新数组$row中
        }        
        $this->assign('row',$row);
        $html = $this->fetch('goods_list');
        $this->success('ok','',$html);
    }
    //Ajax获取第二个下拉框内容
    public function sel2_change($fenlei_id){
        //获取二级分类
        $grade_two_fenlei = db('goods_fenlei')->where("grade=2 and pid={$fenlei_id}")->order('id asc')->select();
        print_r(json_encode($grade_two_fenlei));
    }
}
